<?php
if (!defined("TODAY_RESULT_CLASS_PHS")) {
	define("TODAY_RESULT_CLASS_PHS",1);
	/**
	 * 
	 * @author hungson175
	 * This class is only
	 */
	class TodayResult {
		private $xml=null;
		private $allResults=array();

		/**
		 *
		 * @var integer the released date of lotto number
		 */
		private $resultDate;

		static private	$sogiai =
		array (
		0 => 1,
		1	=> 1,
		2 => 2,
		3 => 6,
		4 => 4,
		5 => 6,
		6 => 3,
		7 => 4
		);

		static public $prizeName =
		array(
		0 => "DB",
		1 => "Nhat",
		2 => "Nhi",
		3 => "Ba",
		4 => "Tu",
		5 => "Nam",
		6 => "Sau",
		7 => "Bay"
		);
		
		
		function TodayResult($filename) {				
			$this -> allResults = $this -> getAllResultsFromXML($filename);
			$this -> date = $this -> getDateFromXML();
		}
		
		private function getAllResultsFromXML($filename) {
			if ( ! $this -> xml = @simplexml_load_file($filename) ) {
				$err = new NoResultFileException();
				$err -> addString('Cannot load file'.$filename);
				throw $err;
			}
			$prize_id = 0;
			$cnt = 0;
			
			$checkSum = 0;				
			foreach($this -> xml -> xsmb -> giai as $aPrize) {
				$cnt++;
				$this -> allResults[$prize_id][] = $aPrize -> attributes();
				if ( $cnt == self::$sogiai[$prize_id] ) {
					$checkSum += $cnt;
					$cnt = 0;
					$prize_id++;
				}
			}
			//echo "CheckSum $checkSum  --- array sum: ",array_sum(self::$sogiai),"\n";
			
			if ($checkSum != array_sum(self::$sogiai) ) {
				$err = new MyException();
				$err ->addString('Result file\'s structure is not right !');
				throw $err;
			}			
			
			return $this->allResults;
		}
		
		private function getDateFromXML() {
			
			$Constant888 = Constant888::instance();
			date_default_timezone_set($Constant888 -> value("TIME_ZONE_STRING"));
						
			$dateString = $this -> xml -> xsmb -> attributes();
			sscanf($dateString,"%d-%d-%d",$day,$month,$year);
			return mktime(0,0,0,$month,$day,$year);
		}
		
		

		/**
		 *
		 * @return mixed the list of prizes,e.g: array( "0"=>array("43382"),"1" => array("234234","21434"), ... )
		 */
		function getPrizeList() {
			return $this -> allResults;
		}

		/**
		 *
		 * @param $pID integer prize ID (0: DB, 1: Nhat .... )
		 * @return mixed an array of the prize $pID,e.g: getResultForPrizeID(1) = array("234234","21434")
		 */
		function getResultForPrizeID($pID) {
			return $this -> allResults[$pID];
		}

		/**
		 *
		 * @return string e.g: "Ngay 08 thang 09 nam 2009"
		 */
		function getDate() {
			list($d,$m,$y) = sscanf(date("d m Y",$this -> date),"%s %s %s");
			$dateString = sprintf("Ngay %s thang %s nam %s",$d,$m,$y);
			return $dateString;
		}
		function showResults_WAP() {

			foreach($this -> allResults as $prizeID => $winNumbers) {

				$aline = self::$prizeName[$prizeID].':';
				$i = 0;
				$n = count($winNumbers);
				foreach($winNumbers as $number) {
					$i++;
					$aline .= $number;
					if ( $i != $n ) $aline .= ',';

				}
				$lines = HelpFunctions::fitToScreen($aline);
				$firstLineElems = explode(':',$lines[0]);		
				
				echo '<p>';
				echo '<b>',$firstLineElems[0],'</b>:';
				echo $firstLineElems[1];
				echo '</p>'."\n";			

				for($i=1; $i < count($lines) ;$i++) {
					echo '<p>',$lines[$i],'</p>';
				}			
				

			}
		}
	}
}
?>
